package cn.iocoder.yudao.module.erp.service.purchaseplan.purchasematerialdetail;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchasematerialdetail.PurchaseMaterialDetailPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchasematerialdetail.PurchaseMaterialDetailSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseplan.purchasematerialdetail.PurchaseMaterialDetailDO;
import cn.iocoder.yudao.module.erp.dal.mysql.purchaseplan.purchasematerialdetail.PurchaseMaterialDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PURCHASE_MATERIAL_DETAIL_NOT_EXISTS;

/**
 * 物料采购计划明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PurchaseMaterialDetailServiceImpl implements PurchaseMaterialDetailService {

    @Resource
    private PurchaseMaterialDetailMapper purchaseMaterialDetailMapper;

    @Override
    public void createPurchaseMaterialDetailBatch(List<PurchaseMaterialDetailSaveReqVO> createReqVOS) {
        // 插入
        List<PurchaseMaterialDetailDO> purchaseMaterialDetailS = BeanUtils.toBean(createReqVOS, PurchaseMaterialDetailDO.class);
        purchaseMaterialDetailMapper.insertBatch(purchaseMaterialDetailS);
    }

    @Override
    public void updatePurchaseMaterialDetail(PurchaseMaterialDetailSaveReqVO updateReqVO) {
        // 校验存在
        validatePurchaseMaterialDetailExists(updateReqVO.getId());
        // 更新
        PurchaseMaterialDetailDO updateObj = BeanUtils.toBean(updateReqVO, PurchaseMaterialDetailDO.class);
        purchaseMaterialDetailMapper.updateById(updateObj);
    }

    @Override
    public void deletePurchaseMaterialDetail(Long id) {
        // 校验存在
        validatePurchaseMaterialDetailExists(id);
        // 删除
        purchaseMaterialDetailMapper.deleteById(id);
    }

    @Override
        public void deletePurchaseMaterialDetailListByIds(List<Long> ids) {
        // 校验存在
        validatePurchaseMaterialDetailExists(ids);
        // 删除
        purchaseMaterialDetailMapper.deleteByIds(ids);
        }

    private void validatePurchaseMaterialDetailExists(List<Long> ids) {
        List<PurchaseMaterialDetailDO> list = purchaseMaterialDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PURCHASE_MATERIAL_DETAIL_NOT_EXISTS);
        }
    }

    private void validatePurchaseMaterialDetailExists(Long id) {
        if (purchaseMaterialDetailMapper.selectById(id) == null) {
            throw exception(PURCHASE_MATERIAL_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public PurchaseMaterialDetailDO getPurchaseMaterialDetail(Long id) {
        return purchaseMaterialDetailMapper.selectById(id);
    }

    @Override
    public PageResult<PurchaseMaterialDetailDO> getPurchaseMaterialDetailPage(PurchaseMaterialDetailPageReqVO pageReqVO) {
        return purchaseMaterialDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByPlanId(Long planId) {
        purchaseMaterialDetailMapper.deleteByPlanId(planId);
    }

    @Override
    public List<PurchaseMaterialDetailDO> getByPlanId(Long planId) {
        return purchaseMaterialDetailMapper.selectByPlanId(planId);
    }

}